package com.vf.admin.client.controller;

import com.vf.admin.client.common.ClientBaseController;
import com.vf.admin.client.service.AppGzUserService;
import com.vf.admin.client.service.AppUserService;
import com.vf.admin.common.entity.Result;
import com.vf.admin.miaodong.entity.AppGzUser;
import com.vf.admin.miaodong.entity.AppUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.math.BigDecimal;

/**
 * <p>Title : </p>
 * <p>Description : </p>
 * <p>DevelopTools : IDEA</p>
 * <p>DevelopSystem : Windows11</p>
 *
 * @Author : zxw
 * @Date 2023/9/6 11:42
 */
@Controller
@RequestMapping("/app/gz")
public class AppGzUserController extends ClientBaseController {

    @Resource
    private AppGzUserService appGzUserService;
    @Autowired
    AppUserService appUserService;

    /**
     * 购买
     *
     * @param userId
     * @return
     */
    @ResponseBody
    @RequestMapping("/goumaiInfo")
    public Result goumaiInfo(Integer userId, BigDecimal diamonds) {
        try {
            Integer userIdToken = getUserId();
            //查询用户信息
            AppUser appUser = appUserService.get(userIdToken);
            boolean b = appUser.getDiamonds().compareTo(diamonds) >= 0;
            if (b) {
                AppGzUser appGzUser = new AppGzUser();
                appGzUser.setUserId(userIdToken);
                appGzUser.setGzUserId(userId);
                appGzUserService.save(appGzUser);
                //扣钻石
                appUserService.upJianZuanShi(userIdToken, diamonds);
                return this.sucessJSON("购买成功");
            }
            return this.errorJSON("余额不足");
//            Integer userIdToken = getUserId();
//            AppGzUser appGzUser = new AppGzUser();
//            appGzUser.setUserId(userIdToken);
//            appGzUser.setGzUserId(userId);
//            appGzUserService.save(appGzUser);
//            //扣钻石
//            appUserService.upJianZuanShi(userId, diamonds);
//            return this.sucessJSON("购买成功");
        } catch (Exception e) {
            return this.errorJSON("系统繁忙，请稍后重试");
        }
    }

    /**
     * 判断是否购买过
     */
    @ResponseBody
    @RequestMapping("/isGouMai")
    public Result isGouMai(Integer userId) {
        try {
            Integer userIdToken = getUserId();
            Boolean gouMai = appGzUserService.isGouMai(userIdToken, userId);
            if (gouMai.equals(true)) {
                return this.sucessJSON(true);
            } else {
                return this.sucessJSON(false);
            }

        } catch (Exception e) {
            return this.errorJSON("系统繁忙，请稍后重试");
        }
    }


}
